// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html
var Lottery28 = [
    {  
        ruleName: "大小单双",  
        colorR:69,
        colorG:120,
        colorB:202,
        //color:"#4578ca",
        content: [  
            {  
                big: "大",
                small: "1:2.0",
                top:"14~27"  
            },  
            {  
                big: "小",
                small: "1:2.0",
                top:"0~13"  
            },  
            {  
                big: "单",
                small: "1:2.0",
                top:"1,3,5,7,9,11,13,15,17,19,21,23,25,27"  
            },  
            {  
                big: "双",
                small: "1:2.0",
                top:"0,2,4,6,8,10,12,14,16,18,20,22,24,26"  
            },  
            {  
                big: "极大",
                small: "1:15.0",
                top:"22,23,24,25,26,27"  
            },  
            {  
                big: "大单",
                small: "1:4.2",
                top:"15,17,19,21,23,25,27"  
            },  
            {  
                big: "小单",
                small: "1:4.6",
                top:"1,3,5,7,9,11,13"  
            }, 
            {  
                big: "大双",
                small: "1:4.6",
                top:"14,16,18,20,22,24,26"  
            }, 
            {  
                big: "小双",
                small: "1:4.2",
                top:"0,2,4,6,8,10,12"  
            }, 
            {  
                big: "极小",
                small: "1:15.0",
                top:"0,1,2,3,4,5"  
            }, 
        ]  
    },  
    {  
        ruleName: "猜数字",
        colorR:191,
        colorG:95,
        colorB:91, 
        content: [ 
            "1:288.0",//0
            "1:188.0",//1
            "1:88.0",//2
            "1:58.0",//3
            "1:38.0",//4
            "1:28.0",//5
            "1:13.0",//6
            "1:13.0",
            "1:13.0",
            "1:13.0",
            "1:13.0",
            "1:13.0",
            "1:12.0",
            "1:12.0",
            "1:12.0",
            "1:12.0",
            "1:13.0",
            "1:13.0",
            "1:13.0",
            "1:13.0",
            "1:13.0",
            "1:13.0",
            "1:28.0",
            "1:38.0",
            "1:58.0",
            "1:88.0",
            "1:188.0",
            "1:288.0",//27
        ]  
    },  
    {  
        ruleName: "特殊玩法",  
        colorR:96,
        colorG:145,
        colorB:55,
        //color:"#609137", 
        selContent:['红','绿','蓝','对子','顺子','豹子'],
        content: [  
            "中奖和值:[3,6,9,15,18,21,24]",  
            "中奖和值:[1,4,7,10,16,19,22,25]",  
            "中奖和值:[2,5,8,11,17,20,23,26]",
            "二个数字一致即中奖",
            "三个数字连号(不分顺序)即中奖",
            "三个数字一致即中奖"
        ]  
    },  
]

var pgName = "PageViewExt";

/**
 * 彩种28控制类
 * 幸运28,加拿大28投注界面 
 */
cc.Class({
    extends: cc.Component,

    properties: {
        labPlaying:cc.Label,//当前玩法名
        pvBet:cc.Node,
        betExplain:cc.Prefab,//赔率说明
        betItem:cc.Prefab,//按钮
        betToggleGroups:[cc.Node],//投注面板中选项组
        betPages:[cc.Node],//投注面板
        spBetBg:cc.Node,//投注面板背景
        EdMoney:cc.Label,//投注金额
        numberKeysPre: cc.Prefab,  //键盘界面预制

        _pageEx:null,//page脚本
        _lotteryCode:0,//彩种id
        _isSpecial:false,//是否幸运28
        _selNum:0,//选择项
        _selData:'',//选择内容
        _keysOpen: false,
        _selList:[]//每页选择项
    },

    onLoad () {
        this._SelData = [];
        this.node.on(cc.Node.EventType.TOUCH_START, function (event) {
            console.log('bet28 TOUCH_START-' + this.node.name);  
        }.bind(this), this);
        this.node.on(cc.Node.EventType.MOUSE_DOWN, function (event) {
            console.log('bet28 MOUSE_DOWN-' + this.node.name);  
        }.bind(this), this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, function (event) {
            console.log('bet28 TOUCH_MOVE-'+ this.node.name);  
        }.bind(this), this);
        this.node.on(cc.Node.EventType.TOUCH_END, function (event) {
            console.log('bet28 TOUCH_END-'+ this.node.name);  
        }.bind(this), this);  
     },

    initReset:function(){

    },

    start () {
        this.node.on(cc.Node.EventType.TOUCH_END, function (event) {
            var contentRect = this.spBetBg.getBoundingBoxToWorld();
            var touchLocation = event.getLocation();
            contentRect.x = contentRect.x -20;
            contentRect.width = 1080;
            if(cc.rectContainsPoint(contentRect, touchLocation) == false){//关闭投注界面
                this.onClose();
            }
        }, this);

        this._pageEx = this.pvBet.getComponent(pgName);//this.pvBet.getComponent(pgName).getComponent(cc.PageView);
        for(var i=0;i<Lottery28.length;i++)
        {
            switch (i)
            {
                case 0://大小单双
                {
                    this.betPages[i].getChildByName("labDec").getComponent(cc.Label).string = "中奖和值:["+Lottery28[i].content[0].top+"]";

                    var group1 = this.betToggleGroups[i];
                    this.labPlaying.string = Lottery28[i].ruleName;
                    for(var j=0;j<Lottery28[i].content.length;j++)
                    {
                        var item = cc.instantiate(this.betItem);
                        item.getChildByName("labBig").getComponent(cc.Label).string = Lottery28[i].content[j].big;
                        item.getChildByName("labSmall").getComponent(cc.Label).string = Lottery28[i].content[j].small;
                        item.parent = group1;
                        item.getComponent(cc.Toggle).toggleGroup = group1.getComponent(cc.ToggleGroup);
                        group1.getComponent(cc.ToggleGroup).addToggle(item.getComponent(cc.Toggle));
                    
                        var checkEventHandler = new cc.Component.EventHandler();
                        checkEventHandler.target = this.node; 
                        checkEventHandler.component = this.node.name;
                        checkEventHandler.handler = "onSelect1";
                        checkEventHandler.customEventData = {
                            sel:Lottery28[i].content[j].big,
                            top:Lottery28[i].content[j].top,
                            num:j,
                            page:i
                        }; 
                        if(j==0)
                        {
                            this._selData = Lottery28[i].content[j].big;
                            this._selNum = j;
                            var obj = new Object();
                            obj.selNum = j;
                            obj.selData = Lottery28[i].content[j].big;
                            this._selList.push(obj);
                        }
                        item.getComponent(cc.Toggle).checkEvents.push(checkEventHandler);
                    }
                }
                break;
                case 1://猜数字
                {
                    this.betPages[i].getChildByName("labDec").getComponent(cc.Label).string = "中奖号码:[0]";

                    var group2 = this.betToggleGroups[i];
                    for(var j=0;j<Lottery28[i].content.length;j++)
                    {
                        var item = cc.instantiate(this.betItem);
                        item.getChildByName("labBig").getComponent(cc.Label).string = j;
                        item.getChildByName("labSmall").getComponent(cc.Label).string = Lottery28[i].content[j];
                        item.parent = group2;
                        item.getComponent(cc.Toggle).toggleGroup = group2.getComponent(cc.ToggleGroup);
                        group2.getComponent(cc.ToggleGroup).addToggle(item.getComponent(cc.Toggle));
                    
                        var checkEventHandler = new cc.Component.EventHandler();
                        checkEventHandler.target = this.node; 
                        checkEventHandler.component = this.node.name;
                        checkEventHandler.handler = "onSelect2";
                        checkEventHandler.customEventData = {
                            num:j,
                            page:i
                        }; 
                        item.getComponent(cc.Toggle).checkEvents.push(checkEventHandler);
                        if(j==0)
                        {
                            var obj = new Object();
                            obj.selNum = j;
                            obj.selData = j;
                            this._selList.push(obj);
                        }
                    }
                }
                break;
                case 2://特殊玩法
                {
                    this.betPages[i].getChildByName("labDec").getComponent(cc.Label).string = Lottery28[i].content[0];
                    var group3 = this.betToggleGroups[i];
                    for(var j=0;j<group3._children.length;j++)
                    {

                        if(!this._isSpecial)
                        {
                            if(j==3 || j==4)
                            {
                                group3._children[j].active = false;
                                continue;
                            }
                        }

                        var checkEventHandler = new cc.Component.EventHandler();
                        checkEventHandler.target = this.node; 
                        checkEventHandler.component = this.node.name;
                        checkEventHandler.handler = "onSelect3";
                       // cc.log("订单："+Lottery28[i].selContent[j]);
                        checkEventHandler.customEventData = {
                            num:Lottery28[i].selContent[j],
                            page:i,
                            index:j
                        }; 
                        group3._children[j].getComponent(cc.Toggle).checkEvents.push(checkEventHandler);
                        if(j==0)
                        {
                            var obj = new Object();
                            obj.selNum = Lottery28[i].selContent[j];
                            obj.selData = Lottery28[i].selContent[j];
                            this._selList.push(obj);
                        }
                    }
                }
                break;
            }
        }
    },

    //第一页选择
    onSelect1:function(event, customEventData){
        this._selNum = customEventData.num;
        this._selData = customEventData.sel;
        this.betPages[customEventData.page].getChildByName("labDec").getComponent(cc.Label).string = "中奖和值:["+customEventData.top+"]";
        this._selList[customEventData.page].selNum = this._selNum;
        this._selList[customEventData.page].selData = this._selData;
    },

    //第二页选择
    onSelect2:function(event, customEventData){
        this._selNum = customEventData.num;
        this._selData = customEventData.num;
        this.betPages[customEventData.page].getChildByName("labDec").getComponent(cc.Label).string = "中奖号码:["+customEventData.num+"]";
        this._selList[customEventData.page].selNum = this._selNum;
        this._selList[customEventData.page].selData = this._selData;
    },

     //第三页选择
    onSelect3:function(event, customEventData){
        this._selNum = customEventData.num;
        this._selData = customEventData.num;
        this.betPages[customEventData.page].getChildByName("labDec").getComponent(cc.Label).string = Lottery28[customEventData.page].content[customEventData.index];
        this._selList[customEventData.page].selNum = this._selNum;
        this._selList[customEventData.page].selData = this._selData;
    },

    init: function(lotterycode){
        this._lotteryCode = lotterycode;
        this._isSpecial = (this._lotteryCode == "40010") ? true:false;
    },

    //赔率说明
    onExplain: function(){
        var canvas = cc.find("Canvas");
        var bet = cc.instantiate(this.betExplain);

        bet.getComponent(bet.name).init('赔率说明',"1.开13/14，总注小于等于10000时，大小单双赔率1.5倍，组合赔率1倍;\n2.开13/14，总注大于10000时，大小单双赔率1倍，组合赔率1倍;\n3.大小单双赔率2倍;\n4.组合（小单|大双）4.6倍;\n5.组合（小双|大单）4.2倍");
        canvas.addChild(bet); 
       
    },

    pageViewClick:function(event,customEventData){
        var pageIndex = event.node.getComponent(pgName).getCurrentPageIndex();//.getComponent(cc.PageView)
        this.labPlaying.string = Lottery28[pageIndex].ruleName;  
        this.spBetBg.color = new cc.Color(Lottery28[pageIndex].colorR,Lottery28[pageIndex].colorG,Lottery28[pageIndex].colorB);
        this._selNum = this._selList[pageIndex].selNum;
        this._selData = this._selList[pageIndex].selData;
        
    },

    onRightPage:function(event,customEventData){
        var pageIndex = this._pageEx.getCurrentPageIndex()+1 == this._pageEx.getPages().length ? 0 : this._pageEx.getCurrentPageIndex()+1;
        this._pageEx.scrollToPage(pageIndex);
        this._selNum = this._selList[pageIndex].selNum;
        this._selData = this._selList[pageIndex].selData;
    },

    onLeftPage:function(event,customEventData){
        var pageIndex = this._pageEx.getCurrentPageIndex()-1 <0 ? this._pageEx.getPages().length-1 : this._pageEx.getCurrentPageIndex()-1;
        this._pageEx.scrollToPage(pageIndex);
        this._selNum = this._selList[pageIndex].selNum;
        this._selData = this._selList[pageIndex].selData;
    },

    //最小投注
    onMinBet:function(){
        this.EdMoney.string = "10";
    },

    //双倍投注
    onDoubleBet:function(){
        if(this.EdMoney.string != "")
        {
            var num = parseInt(this.EdMoney.string)*2; 
            var obj = this.checkBetMoneny(num);
            this.EdMoney.string = (obj.num).toString();
        }
    },

    //检查投注金额是否合规
    checkBetMoneny:function(num){
        var retNum = num;
        var isCheck = false;
        switch (this._pageEx.getCurrentPageIndex())
        {
            case 0://大小单双
            {
                if(this._selNum>=4)
                {
                    retNum = (num<=10000)?num:10000;
                }
                else
                {
                    retNum = (num<=20000)?num:20000;
                }
            }
            break;
            case 1://猜数字
            {
                retNum = (num<=5000)?num:5000;
            }
            break;
            case 2://特殊玩法
            {
                retNum = (num<=10000)?num:10000;
            }
            break;
        }

        return {num:retNum,check:(retNum == num)?true:false};
    },

    //开启键盘 
    onTextBegan: function(){
        this.emptyNode =this.node.getChildByName('Node');
        if(!this.emptyNode.getChildByName('public_numberKeys_pop')){  //当键盘存在，就不必创建
            this.KeysPre =cc.instantiate(this.numberKeysPre);
            this.emptyNode.addChild(this.KeysPre);
            var Callback = function(data){
            this.onEditBoxChanged(data);
            }.bind(this);
            this.KeysPre.getComponent(this.KeysPre.name).init(Callback);
            this.KeysPre.getComponent(this.KeysPre.name).onShow();  //显示键盘界面  
        }else{
            this.KeysPre.getComponent(this.KeysPre.name).onShow();  //显示键盘界面      
        }
        this._keysOpen =true;  //键盘开启
    },

    onEditBoxChanged:function(text){
        var amount =text;
        if(amount =='ok'){
            // this.emptyNode.getComponent(cc.Layout).enabled = false; 
            this.KeysPre.getComponent(this.KeysPre.name).onClose();  //清除键盘界面  
            this._keysOpen =false;   
        }else if(amount =='-'){
            if(this.EdMoney.string.length > 0){
                this.EdMoney.string =this.EdMoney.string.slice(0,-1);  
            }        
        }else{
            this.EdMoney.string += amount;  
            // if(this.EdMoney.string.length>6){   //只能输入6位
            //     this.EdMoney.string =this.EdMoney.string.slice(0,6);  
            // }
            var obj = this.checkBetMoneny(Number(this.EdMoney.string));
            if(!obj.check)
            {
                ComponentsUtils.showTips("投注金额与当前玩法不符合");
                this.EdMoney.string ="";
                return;
            }
        }  
    },

    //投注信息组合
    dataToJson:function(){
        var obj = new Object(); 
        var palycode = "";
        var lycode = this._lotteryCode + '0';
        var pageIndex = this._pageEx.getCurrentPageIndex();
        palycode = lycode + (pageIndex+1);

        obj.PlayCode = palycode; 
        var arry = [];
        var numArrays = {
                "Number":this._selData
            };
        arry.push(numArrays);
        obj.Data = arry;
        var json = JSON.stringify(obj);
        cc.log("bet 提交订单：" + json);
        return json; 
    },

    //付款
    onPayBtn:function(){
        if(!Utils.isInt(this.EdMoney.string))
        {
            ComponentsUtils.showTips("只能投注整数倍");
            return;
        }
        var moy = parseInt(this.EdMoney.string);
        if(moy < 10)
        {
            ComponentsUtils.showTips("最低需要投注10");
            return;
        }
        var obj = this.checkBetMoneny(moy);
        if(!obj.check)
        {
            ComponentsUtils.showTips("投注金额与当前玩法不符合");
            return;
        }
            
        
        var data = {
            lotteryId:this.lotteryID,//彩种id
            dataBase:this.dataToJson(),//投注信息
            money:this.EdMoney.string,
            buyType: 0,//追号
            betType: DEFINE.BET_PAY_TYPE.SPECIAL//投注类型
        }
        window.Notification.emit("BET_ONPAY",data);

        //关闭键盘界面
        if(!this._keysOpen){
            return;
        }
        if(this.KeysPre){  
            this.KeysPre.getComponent(this.KeysPre.name).onClose();  //清除键盘界面 
            this._keysOpen =false;
        }
    },

    onClose:function(){
        window.Notification.emit("BET_ONCLOSE","");
        //关闭键盘界面
        if(!this._keysOpen){
            return;
        }
        if(this.KeysPre){  
            this.KeysPre.getComponent(this.KeysPre.name).onClose();  //清除键盘界面 
            this._keysOpen =false;
        }
    }
});
